package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.maiji.cloud.entities.login.AppCarousel;
import com.maiji.cloud.entities.login.UploadRecord;
import com.maiji.cloud.mapper.AppCarouselMapper;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.UploadImageResData;
import com.maiji.cloud.service.AppCarouselService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.service.UploadRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class AppCarouselServiceImpl extends ServiceImpl<AppCarouselMapper, AppCarousel> implements AppCarouselService {

    @Autowired
    private UploadRecordService uploadRecordService;

    @Override
    public BaseDataResDto<List<UploadImageResData>> findAppCarousel(String name) {
        AppCarousel appCarousel = selectOne(new EntityWrapper<AppCarousel>().eq("name", name));
        if (appCarousel == null) return new BaseDataResDto<>(Status.PARAMETERERROR);
        EntityWrapper<UploadRecord> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("belong_id", appCarousel.getUuId());
        entityWrapper.orderBy("no", true);
        List<UploadRecord> uploadRecords = uploadRecordService.selectList(entityWrapper);
        List<UploadImageResData> data = uploadRecords.stream().map(uploadRecord -> {
            UploadImageResData uploadImageResData = new UploadImageResData();
            BeanUtils.copyProperties(uploadRecord, uploadImageResData);
            return uploadImageResData;
        }).collect(Collectors.toList());
        return new BaseDataResDto<List<UploadImageResData>>(Status.SUCCESS).setData(data);
    }

}
